package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

@SuppressWarnings("serial")
public class ContactServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		String[] check = new String[6];
		request.setAttribute("check", check);
		try {
			request.getRequestDispatcher("/contact.jsp").forward(request,
					response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		
		String[] check = new String[6];
		
		String name = request.getParameter("name");
	    String email = request.getParameter("email");
	    String title = request.getParameter("title");
	    String website = request.getParameter("website");
	    String message = request.getParameter("message");
		
	    String text = "Name : "+name+"<br/>"+"Website : "+website+"<br/>Email : "+email+"<br/>"+message;
	    
	    if(name == null || (name != null && name.equals("")))
	    {
	    	check[0] = "Name is not empty";
	    }
	    if(title == null || (title != null && title.equals("")))
	    {
	    	check[4] = "Title is not empty";
	    }
	    
	    java.util.regex.Pattern p = java.util.regex.Pattern.compile(".+@.+\\.[a-z]+");
	    Matcher m = p.matcher(email);
	    boolean matchFound = m.matches();

	    if (!matchFound)
	    {
	    	check[1] = "Invalid Email";
	    }
	    if(message == null || (message != null && message.equals("")))
	    {
	    	check[2] = "Message is not empty";
	    }
	    
		//check capchar
		String remoteAddr = request.getRemoteAddr();
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey("6LcF-80SAAAAAM2kArjyUwtVJYbH_DKredY3Q26U");

        String challenge = request.getParameter("recaptcha_challenge_field");
        String uresponse = request.getParameter("recaptcha_response_field");
        if(challenge == null)
        {
        	challenge = "b";
        }
        if(uresponse == null)
        {
        	uresponse = "a";
        }
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

        if (!reCaptchaResponse.isValid()) {
        	check[3] = "Invalid Capchar";
        }
		if(check[0] == null && check[1] == null && check[2] == null && check[3] == null&&check[4] == null&&check[5] == null)
		{
			try {
	            URL url = new URL("http://send-emails.appspot.com/sendemail");
	            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	            connection.setDoOutput(true);
	            connection.setRequestMethod("POST");
	            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
	            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
	            writer.write("email=admin@hatforrent.com");
	            writer.write("&name_email="+email);
	            writer.write("&from_email=admin@hatforrent.com");
	            writer.write("&subject=Mail contact from hatforrent.com | "+title);
	            writer.write("&content="+text);
	            writer.close();
	    
	            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	            	 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            }
			} catch (Exception e) {
				e.printStackTrace();
			}
			request.setAttribute("success", "Thank you for your contact");
		} else {
			request.setAttribute("name", name);
			request.setAttribute("email", email);
			request.setAttribute("title", title);
			request.setAttribute("website", website);
			request.setAttribute("message", message);
		}
		request.setAttribute("check", check);
		try {
			request.getRequestDispatcher("/contact.jsp").forward(request,
					response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
